'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Net
Imports System.Windows.Forms

Public Class AAPT
    Public ISPReturnData As New ISPReturnData
    Private CookieContainer As New System.Net.CookieContainer

    Public Sub New(ByVal ConfigurationData As ConfigurationData)

        Try
            'set rollover date
            SetRolloverDate(ConfigurationData, ISPReturnData)

            Dim strHTMLData As String

            'create credentials
            Dim cCache As New CredentialCache
            cCache.Add(New Uri("https://dialup.connect.com.au/cgi-bin/subscribers/usagepd.cgi?tuser=" & ConfigurationData.Username), "Basic", New NetworkCredential(ConfigurationData.Username, ConfigurationData.Password))

            'login
            strHTMLData = ISPGet("https://dialup.connect.com.au/cgi-bin/subscribers/usagepd.cgi?tuser=" & ConfigurationData.Username, CookieContainer, cCache)

            'process response
            If strHTMLData = "" Then
                ISPReturnData.ErrorMessage = "Login Failed"
                Exit Sub
            End If

            'usage
            Dim URL As String = "https://dialup.connect.com.au/cgi-bin/subscribers/usagepd.cgi"
            Dim POSTData As String = "c_type=5&start_date=1&start_month=" & Now.Month & "&start_year=" & Now.Year & "&end_date=" & Now.Day & "&end_month=" & Now.Month & "&end_year=" & Now.Year & "&action=Show+Connections&tuser="
            strHTMLData = ISPPost(URL, POSTData, CookieContainer, cCache)

            If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

            ISPReturnData.PeakDownload = CInt(Val(ExtractText(strHTMLData, "Overall Totals :", 0, "<B>", " MB")))
            ISPReturnData.LastUpdated = Now

            '##########################################################

            ProcessISPData(ConfigurationData, ISPReturnData)

            'check for error
            If ISPReturnData.ErrorMessage <> "" Then Exit Sub

            '##########################################################

            'create graph data

            'downloads
            ISPReturnData.GraphItemList.Add(New GraphItem("Downloads " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), False))

            'days elapsed
            ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

            '##########################################################

            'create stats data
            ISPReturnData.TextItemList.Add(New TextItem("D/L Left :", ISPReturnData.PeakUsageRemainingDescription))
            ISPReturnData.TextItemList.Add(New TextItem("D/L Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))

            If ISPReturnData.ShapedNow = False Then
                ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "NO"))
            Else
                ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "YES"))
            End If

            '##########################################################

            'process alerts
            ProcessAlerts(ConfigurationData, ISPReturnData)

        Catch ex As Exception
            ISPReturnData.ErrorMessage = ex.Message
        End Try
    End Sub

End Class
